package imnu.dbuilts;

import imnu.pojo.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: lys
 * @类名: TestOfDBUilts
 * @Title:
 * @date: 2020/8/12
 **/
public class TestOfDBUilts {
    public static void main(String[] args) {
//        test01();
//        test02();
        test03();
    }

    public static void test03() {
        String strSql = "INSERT INTO depts(dept_id,name,description) VALUES(?,?,?);";
        Object[] objects = {13,"质检部","最后保障"};
        int i = DBProvider.update(strSql,objects);
//        int i = DBProvider.update(strSql,10,"测试部","检查漏洞");
        if (i>0){
            System.out.println("新增数据成功！");
        }else{
            System.out.println("新增数据失败！");
        }
    }

    public static void test02() {
        String strSql = "select * from tb_student;";
        List<Student> list = DBProvider.query(strSql, new BeanListHandler<>(Student.class));
        List<Map<String,Object>> listMap = DBProvider.query(strSql,new MapListHandler());
        List<Object[]> listArray = DBProvider.query(strSql,new ArrayListHandler());
        for (Student eachBean:list){
            System.out.print("    "+eachBean.getTid());
            System.out.print("    "+eachBean.getName());
            System.out.println();
        }


    }

    public static void test01() {
        String strSql = "select * from employees;";
        QueryRunner runner = new QueryRunner(DBCPDataSource.getDataSource());
        try {
            List<Map<String,Object>> list =  runner.query(strSql,new MapListHandler());
            for (Map<String,Object> mapEachRow : list) {
                Set<Map.Entry<String,Object>> setEn =  mapEachRow.entrySet();
                for(Map.Entry<String,Object> en:setEn){
                    System.out.print("    "+en.getValue());
                }
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
